using UnityEngine;
using System.Collections;

/// \class  gkAbyssalGiantBackground
/// \brief  Scroll a texture accordingly to the scrolling info
[AddComponentMenu("GK/Engine/Game/Scrolling/gkScrollingTexture")]
public class gkScrollingTexture : MonoBehaviour 
{
    /// The parallax coeff
    public float ParallaxCoeff = 0.25f;

    /// The scrolling info
    private gkScrollingInfo m_rScrollingInfo;

    /// \brief  Called just after the game begin to play
    void Start()
    {
	    // Grab the scrolling info
	    m_rScrollingInfo = GameObject.Find("ScrollingInfo").GetComponent<gkScrollingInfo>();
    }

    /// \brief  Called each frame
    void Update()
    {
	    UpdateScrolling();
    }

    /// \brief  Update the UV to match the scrolling
    void UpdateScrolling()
    {
	    Vector3 fUVMovement;
    	
	    // Get the Scrolling speed
	    fUVMovement = -m_rScrollingInfo.GetScrollingTotalMovement() * ParallaxCoeff;

	    // Change the uv to match the scrolling
        renderer.material.SetTextureOffset("_MainTex", new Vector2(fUVMovement.x / renderer.bounds.size.x,
            fUVMovement.y / renderer.bounds.size.y));
    }
}
